import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.UIManager;


public class databasePopup extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	/*
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					databasePopup frame = new databasePopup();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	*/

	/**
	 * Create the frame.
	 */
	public databasePopup(ResultSet result) {
		int colCount = 0;
		ResultSetMetaData metaData;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 432, 315);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
	try 
	{
		metaData = result.getMetaData();
	    colCount = metaData.getColumnCount();
	        
		JTable table = new JTable(buildTableModel(result));
		//table = new JTable(colCount, 6);
		table.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
	
	} catch (SQLException e) 
	{
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		//table.set
		/*
		/
		*/
		table.setBounds(15, 16, 375, 80);
		contentPane.add(table);
	}
	
	//public static void newTable()


public DefaultTableModel buildTableModel(ResultSet result)
        throws SQLException {
	//int currRow = -1;

    ResultSetMetaData metaData = result.getMetaData();

    // names of columns
    Vector<String> columnNames = new Vector<String>();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }
    
   
    //result.last();
    //System.out.println(result.getRow());
    /*
    if(result.getRow() > 4)
    	
    {
        for(int i = 0; i < 5; i++)
        {
        	result.previous();
        }
    }
    else
    	currRow = result.getRow();
    {
    	for(int n = 0; n < currRow; n++)
    	{
    		result.previous();
    	}
    }
    currRow = result.getRow();
    */
   /// result.previous();
    System.out.println(result.getRow());
    // data of the table
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    while (result.next()) {
    	Vector<Object> vector = new Vector<Object>();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(result.getObject(columnIndex));
        }
        data.add(vector);
    }

    return new DefaultTableModel(data, columnNames);

}


}
